<?php
	/**
	 * 公共控制器
	 */
	class CommonAction extends Action{
		/**
		 * 判断用户是否登录
		 */
		public function _initialize(){
			if(!isset($_SESSION['uid']) || !isset($_SESSION['username'])){
				// redirect(U('Login/index'));
			}
		}

		//uploadify上传品牌LOGO处理
		public function uploadBrand(){
			$savePath = './Uploads/BrandImgs/';
			function uploadOver($data){
				$status = $data['status'];
				$msg = $data['msg'];
				switch ($status) {
					case 1:
						echo json_encode(array('status' => 1, 'path' => $msg[0]['savename']));die;
						break;
					case 2:
						echo json_encode(array('status' => 2, 'msg' => $msg));die;
						break;
					case 0:
						echo json_encode(array('status' => 0, 'msg' => $msg));die;
						break;
					default:
						break;
				}
			}
			//调用自定义公共上传函数
			$this->uploadSelfDefine($savePath , 'uploadOver' , false);
		}

		//uploadify上传商品列表图
		public function uploadListImg(){
			$savePath = './Uploads/ListImgs/';
			function uploadOver($data){
				$status = $data['status'];
				$msg = $data['msg'];
				switch ($status) {
					case 1:
						echo json_encode(array('status' => 1, 'path' => $msg[0]['savename']));die;
						break;
					case 2:
						echo json_encode(array('status' => 2, 'msg' => $msg));die;
						break;
					case 0:
						echo json_encode(array('status' => 0, 'msg' => $msg));die;
						break;
					default:
						break;
				}
			}
			//调用自定义公共上传函数
			$this->uploadSelfDefine($savePath , 'uploadOver' , false);
		}

		//uploadify上传商品图册
		public function uploadGoodsImg(){
			$savePath = './Uploads/GoodsImgs/';
			$thumbPath = './Uploads/GoodsImgs/' . date('Y-m') . '/';
			function uploadOver($data){
				$status = $data['status'];
				$msg = $data['msg'];
				switch ($status) {
					case 1:
						echo json_encode(array('status' => 1, 'path' => $msg[0]));die;
						break;
					case 2:
						echo json_encode(array('status' => 2, 'msg' => $msg));die;
						break;
					case 0:
						echo json_encode(array('status' => 0, 'msg' => $msg));die;
						break;
					default:
						break;
				}
			}
			//调用自定义公共上传函数
			$this->uploadSelfDefine($savePath , 'uploadOver' , true ,'460,60' , '460,60' , 'medium_,mini_' , $thumbPath);
		}

		/**
		 * 自定义公共上传处理函数
		 * $savePath 上传文件保存路径
		 * $callback 上传完成执行的回调函数
		 * $thumb 	 是否生成缩略图
		 */
		public function uploadSelfDefine($savePath,$callback=null,$thumb,$thumbMaxWidth=0,$thumbMaxHeight=0,$thumbPrefix=null,$thumbPath=null){
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize = 2000000 ;	//最大上传大小
			$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//充许上传文件的类型
			$upload->saveRule = 'uniqid';
			$upload->savePath = $savePath;//文件保存路径
			$upload->autoSub = true;//使用子目录保存
			$upload->subType = 'date';//使用日期子目录
			$upload->dateFormat = 'Y-m';//按月份保存
			$upload->thumb = $thumb;	//生成缩略图
			$upload->thumbMaxWidth = $thumbMaxWidth; //缩略图宽度
			$upload->thumbMaxHeight = $thumbMaxHeight;	//缩略图高度
			$upload->thumbPrefix = $thumbPrefix;	//缩略图前缀名
			$upload->uploadReplace = true;	//覆盖原图
			$upload->thumbRemoveOrigin = false;
			$upload->thumbPath = $thumbPath;
			if(!is_dir($upload->savePath)){
				if(!mkdir($upload->savePath , 0777 , true)){
					if(!is_null($callback)){//创建目录失败
						$loadMsg = array('status' => 2 ,'msg' => '创建上传目录失败，请查看目录读写权限！');
						$callback($loadMsg);
					}
				}
			}
			if ($upload->upload()) {
				if(!is_null($callback)){//上传成功
					$uploadMsg = $upload->getUploadFileInfo();
					if($upload->thumb){
						$uploadImgs = explode('/', $uploadMsg[0]['savename']);
						$uploadMsg[0]['medium'] = $uploadMsg[0]['savePath'] .$uploadImgs[0].'/medium_'. $uploadImgs[1];
						$uploadMsg[0]['mini'] = $uploadMsg[0]['savePath'] .$uploadImgs[0].'/mini_'. $uploadImgs[1];
					}
					$loadMsg = array('status' => 1, 'msg' => $uploadMsg);
					$callback($loadMsg);
				}
			} else {
				if(!is_null($callback)){//上传出错
					$loadMsg = array('status' => 0, 'msg' => $upload->getErrorMsg());
					$callback($loadMsg);
				}
			}
		}

		//uedit编辑器商品介绍图片上传处理
		public function intro(){
			header("Content-Type: text/html; charset=utf-8");
	    	error_reporting(E_ERROR | E_WARNING);
	    	$title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
			$savePath = './Uploads/intro/';
			//上传完成后执行的回调函数
			function uploadOver($data){
				$status = $data['status'];
				$msg = $data['msg'];
				switch ($status) {
					case 1:
						echo json_encode(array(
							'url' => $msg[0]['savename'],
							'title' => $title,
							'state' => 'SUCCESS'
							));die;
						break;
					case 2:
						echo json_encode(array('status' => 2, 'msg' => $msg));die;
						break;
					case 0:
						echo json_encode(array('status' => 0, 'msg' => $msg));die;
						break;
					default:
						break;
				}
			}
			//调用自定义公共上传函数
			$this->uploadSelfDefine($savePath , 'uploadOver' , false);
		}
	}
?>